package com.shadow.ui.progress.circle;

import android.content.Context;
import android.support.v7.app.AppCompatDialog;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.github.lzyzsd.circleprogress.ArcProgress;
import com.shadow.core.R;
import com.shadow.core.util.screen.ScreenUtils;


/**
 * @author zhangyu
 * @date 2018/3/7
 */

public final class CircleProgressLoader {
    private static final int LOADER_SIZE_SCALE = 4;
    private static final int LOADER_OFFSET_SCALE = 10;
    private ArcProgress mArcProgress;
    private Context mContext;
    private AppCompatDialog mDialog;

    public CircleProgressLoader(Context context) {
        mContext = context;
    }

    public final void showProgress() {
        if (mDialog == null) {
            mDialog = new AppCompatDialog(mContext, R.style.dialog);
            mDialog.setCanceledOnTouchOutside(false);
            View view = CircleProgressCreator.createView(mContext);
            mArcProgress = view.findViewById(R.id.arc_progress);
            mDialog.setContentView(view);

            int deviceWidth = ScreenUtils.getScreenWidth();
            int deviceHeight = ScreenUtils.getScreenHeight();

            final Window window = mDialog.getWindow();
            if (window != null) {
                final WindowManager.LayoutParams lp = window.getAttributes();
                lp.width = deviceWidth / LOADER_SIZE_SCALE;
                lp.height = deviceHeight / LOADER_SIZE_SCALE;
                lp.height = lp.height + deviceHeight / LOADER_OFFSET_SCALE;
                lp.gravity = Gravity.CENTER;
            }
        }
        mDialog.show();
        setPercent(0);
    }

    public final void setPercent(int percent) {
        if (mArcProgress != null) {
            mArcProgress.setProgress(percent);
        }
    }

    public final void disMiss() {
        mDialog.dismiss();
    }

}
